package com.briup.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.briup.BO.AnalysisCountBO;
import com.briup.VO.AnalysisCountSalesVO;
import com.briup.mapper.AnalysisMapper;
import com.briup.mapper.AnimalMapper;
import com.briup.mapper.RecordMapper;
import com.briup.pojo.Animal;
import com.briup.pojo.Record;
import com.briup.result.Result;
import com.briup.service.AnalysisService;
import com.google.common.collect.ImmutableMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class AnalysisServiceImpl implements AnalysisService {
    //注入Analysis Mapper层对象
    @Autowired
    private AnalysisMapper analysisMapper;
    @Autowired
    private AnimalMapper animalMapper;
    @Autowired
    private RecordMapper recordMapper;

    @Override
    public Result count() {
        List<AnalysisCountBO> analysisCountBOS = analysisMapper.count();
        Map<String, List> map = new HashMap<>();
        List<String> name = new ArrayList<>();
        List<Integer> value = new ArrayList<>();
        for (AnalysisCountBO analysisCountBO : analysisCountBOS) {
            name.add(analysisCountBO.getName());
            value.add(analysisCountBO.getCount());
        }
        map.put("name", name);
        map.put("value", value);
        return Result.success(map);
    }

    @Override
    public Result indexCount() {
        QueryWrapper<Animal> animalQueryWrapper1 = new QueryWrapper<>();
        animalQueryWrapper1.le("a_weight", 30);
        QueryWrapper<Animal> animalQueryWrapper2 = new QueryWrapper<>();
        animalQueryWrapper1.gt("a_weight", 30);
        QueryWrapper<Animal> animalQueryWrapper3 = new QueryWrapper<>();
        animalQueryWrapper1.between("a_weight", 30, 50);
        Long less30 = animalMapper.selectCount(animalQueryWrapper1);
        Long greaterThan50 = animalMapper.selectCount(animalQueryWrapper2);
        Long between30And50 = animalMapper.selectCount(animalQueryWrapper3);
        Map<String, Long> stringLongMap = new TreeMap<>();
        stringLongMap.put("30以下", less30);
        stringLongMap.put("50以上", greaterThan50);
        stringLongMap.put("30~50", between30And50);
        return Result.success(stringLongMap);
    }

    @Override
    public Result countDisease() {
        QueryWrapper<Record> diseaseQueryWrapper1 = new QueryWrapper<>();
        diseaseQueryWrapper1.eq("dr_desc", "流行性感冒");
        QueryWrapper<Record> diseaseQueryWrapper2 = new QueryWrapper<>();
        diseaseQueryWrapper1.eq("dr_desc", "传染性胃肠炎");
        QueryWrapper<Record> diseaseQueryWrapper3 = new QueryWrapper<>();
        diseaseQueryWrapper1.eq("dr_desc", "维生素A缺乏症");
        Long a = recordMapper.selectCount(diseaseQueryWrapper1);
        Long b = recordMapper.selectCount(diseaseQueryWrapper2);
        Long c = recordMapper.selectCount(diseaseQueryWrapper3);
        Map<String, Long> stringLongMap = new TreeMap<>();
        stringLongMap.put("流行性感冒", a);
        stringLongMap.put("传染性胃肠炎", b);
        stringLongMap.put("维生素A缺乏症", c);
        return Result.success(stringLongMap);
    }

    @Override
    public Result countSales() {
        List<AnalysisCountSalesVO> analysisCountVOS = analysisMapper.countSales();
        List<String> names = Arrays.asList("1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月");
        //初始化values集合
        List<Integer> values = Arrays.asList(new Integer[12]);
        for (int i = 0; i < analysisCountVOS.size(); i++) {
            //将存在的出单月份对应数量放在values对应位置上
            values.set(analysisCountVOS.get(i).getMonth().intValue() - 1, analysisCountVOS.get(i).getCount());
        }
        //为values的空值设置默认值
        for (int i = 0; i < values.size(); i++) {
            if (Objects.isNull(values.get(i))) {
                //设置默认值为0
                values.set(i, 0);
            }
        }
        Map<String, List> map = ImmutableMap.of("name", names, "value", values);
        return Result.success(map);
    }
}

